package com.whq.mvc.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AmqpConfig {

    //商品库存队列
    public static final String QUEUE_ITEM_STOCK = "queue_item_stock";

    public static final String MS_EXCHANGE = "ms_exchange";

    public static final String DEDUCT_ROUTEINGKEY = "deduct";

    @Bean
    public Queue itemStock(){
        return new Queue(QUEUE_ITEM_STOCK);
    }

    @Bean
    public DirectExchange msExchange(){
        return new DirectExchange(MS_EXCHANGE);
    }

    @Bean
    public Binding bindingDirect(){
        return BindingBuilder.bind(itemStock()).to(msExchange()).with(DEDUCT_ROUTEINGKEY);
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.declareExchange(msExchange());
        rabbitAdmin.declareQueue(itemStock());
        rabbitAdmin.declareBinding(bindingDirect());
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }
}
